﻿Imports LRS.BLL
Imports LRS.DTO
Imports System.Windows.Forms

Public Class ManageProduct
    Private IsEditing As Boolean = True
    Private Sub ManageProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dataGridProduct.AutoGenerateColumns = False
        dataGridProduct.DataSource = bProduct.GetProducts()
    End Sub
    Private oldRowIndex As Integer = -1
    Private Sub dataGridProduct_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dataGridProduct.RowEnter
        If (e.RowIndex = oldRowIndex) Then
            Return
        Else
            oldRowIndex = e.RowIndex
        End If

        Dim productID As Integer = dataGridProduct.Rows(e.RowIndex).Cells("ProductID").Value
        Dim product As eProduct = bProduct.GetProductByID(productID)
        ucProduct.SetProduct(product)
        ucProduct.Enabled = True
    End Sub

    Private Sub btnAddProduct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddProduct.Click
        dataGridProduct.Enabled = False
        btnDeleteProduct.Enabled = False
        btnAddProduct.Enabled = True
        btnCancel.Enabled = True
        IsEditing = False
        ucProduct.ClearText()
        ucProduct.Enabled = True
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        btnAddProduct.Enabled = True
        dataGridProduct.Enabled = True
        btnDeleteProduct.Enabled = True
        btnCancel.Enabled = False
        ucProduct.Enabled = False
        IsEditing = True
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim product As eProduct = ucProduct.GetProduct()
        If (product Is Nothing) Then
            Return
        End If
        If (IsEditing) Then
            bProduct.Update(product)
        Else
            bProduct.Insert(product)
        End If
        
        dataGridProduct.Enabled = True
        btnCancel.Enabled = False
        btnDeleteProduct.Enabled = True
        Dim i = oldRowIndex
        oldRowIndex = -1
        ucProduct.Enabled = False
        dataGridProduct.DataSource = Nothing
        dataGridProduct.DataSource = bProduct.GetProducts()
        dataGridProduct.Rows(i).Cells("ProductNumber").Selected = True

    End Sub

    Private Sub btnDeleteProduct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteProduct.Click
        If (dataGridProduct.SelectedCells.Count = 0) Then
            MessageBox.Show("Vui lòng chọn dòng cần xóa")
            Return
        End If
        Dim row As Integer = dataGridProduct.SelectedCells(0).RowIndex
        Dim result As DialogResult
        result = MessageBox.Show("Bạn có chắc chắn muốn xóa sản phẩm có Số hiệu: " & dataGridProduct.Rows(row).Cells("ProductNumber").Value, "Xác nhận", MessageBoxButtons.OKCancel)
        If (result = DialogResult.OK) Then
            Dim productID As Integer = dataGridProduct.Rows(row).Cells("ProductID").Value
            bProduct.DeleteProductByID(productID)
            dataGridProduct.DataSource = Nothing
            dataGridProduct.DataSource = bProduct.GetProducts()
            oldRowIndex = -1
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class